const express = require("express");
const pool = require("../pool.js");

const artRouter = express.Router();
module.exports = artRouter;

// 插入文章
artRouter.post("/insert", (req, res, next) => {
  var title = req.body.title;
  var sketch = req.body.sketch;
  var arttime = req.body.arttime;
  var content = req.body.content;
  if (title == "" || sketch == "" || arttime == "") {
    res.send({ code: 202, msg: "输入不可为空" });
    return;
  }
  let sql1 = "SELECT artid FROM article WHERE title=?";
  pool.query(sql1, [title], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      res.send({ code: 201, msg: "标题重复" });
    } else {
      var sql2 = `insert into article values(null,?,?,?,?)`;
      pool.query(sql2, [title, sketch, arttime, content], (err, r) => {
        if (err) {
          next(err);
          return;
        }
        if (r.affectedRows != 0) {
          res.send({ code: 200, msg: "发布文章成功" });
        } else res.send({ code: 201, msg: "发布文章失败" });
      });
    }
  });
});

// 删除文章
artRouter.delete("/delete", (req, res, next) => {
  var title = req.query.title;
  var sql1 = `SELECT artid FROM article where title=?`;
  console.log(title);
  pool.query(sql1, [title], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      var sql2 = `delete from article where title=?`;
      pool.query(sql2, [title], (err, r) => {
        if (err) {
          next(err);
          return;
        }
        res.send({ code: 200, msg: "删除成功" });
      });
    } else {
      res.send({ code: 201, msg: "找不到这篇文章" });
    }
  });
});

// 查找文章
artRouter.get("/detail", (req, res, next) => {
  var artid = req.query.artid;
  sql = `SELECT * FROM article where artid=?`;
  pool.query(sql, [artid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      res.send({ code: 200, msg: "查找成功", data: r[0] });
    } else {
      res.send({ code: 201, msg: "找不到该文章" });
    }
  });
});

// 查找所有文章的id、标题和概要及发布时间
artRouter.get("/findAll", (req, res, next) => {
  var page = req.query.page || 1;
  const pagesize = parseInt(req.query.pagesize) || 8;
  var offset = (page - 1) * pagesize;
  var sql = `select count(artid) as count from article`;
  pool.query(sql, (err, r) => {
    if (err) {
      next(err);
      return;
    }
    var artcount = r[0].count;
    let pagecount = Math.ceil(artcount / pagesize);
    sql = `SELECT artid,title,sketch,arttime FROM article limit ?,?`;
    pool.query(sql, [offset, pagesize], (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, data: r, pagecount });
    });
  });
});

// 修改文章的标题、概要及发布时间
artRouter.put("/update", (req, res, next) => {
  var oldtitle = req.body.oldtitle;
  var newtitle = req.body.newtitle;
  var sketch = req.body.sketch;
  var arttime = req.body.arttime;
  var sql1 = "select artid from article where title=?";
  pool.query(sql1, [oldtitle], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      var sql2 =
        "update article set title=?, sketch=?, arttime=? where title=?";
      pool.query(sql2, [newtitle, sketch, arttime, oldtitle], (err, r) => {
        if (err) {
          next(err);
          return;
        }
        if (r.affectedRows != 0) res.send({ code: 200, msg: "修改成功" });
        else res.send({ code: 201, msg: "修改失败" });
      });
    } else {
      res.send({ code: 202, msg: "标题不存在" });
    }
  });
});
